<?php
/**
 * Ajax actions
 *
 * @package    colibri
 * @subpackage Ajax
 * @author     Andres Lartigue
 * @version    v2.4.1
 */
class ajaxActions extends sfActions
{
	  public function executeGetListDossiers(sfWebRequest $request){

	  	sfConfig::set('sf_web_debug', false);
		$i18N = sfContext::getInstance()->getI18N();
		$sf_user = $this->getUser();
		$this->personne=$sf_user->getUserProfile();
		$this->moduleRef = $request->getParameter('moduleRef');
		$this->actionRef = $request->getParameter('actionRef');
		$this->view = $request->getParameter('view');
		$this->className = 'dossier';
		$nbpage = (int)$request->getParameter('page',1);

		$search = new searchBar();
		
		$this->objDef = new objectDef('dossier');
		
		$this->config = ambnoyFuncs::getViewConfig($this->view, $this->personne, $this->objDef);
		
		
		$this->nbPosts = $request->getParameter('nbPosts');
		if(!isset($this->nbPosts)){
			$this->nbPosts = sfConfig::get('app_posts_number_per_page', 10);
		}
		
		$orderByCol = $request->getParameter('order',$this->config['orderColumn']);
		if(trim($orderByCol)=='') $orderByCol = 'id*desc';
		
	
		/*$sf_user->setAttribute('returnButton','');
		$viewOriginal = $sf_user->getAttribute('viewOriginal');
		if($viewOriginal!=$this->view){
			$sf_user->getAttributeHolder()->remove('activeFilters');
			$sf_user->setAttribute('viewOriginal',$this->view);
		}*/
		
		$sf_user->setAttribute('viewPage',$nbpage);

		$strConfig = $this->personne->getParam('viewConfig/' . $this->className.'/'.$this->view, true);
		if($strConfig){
			//Config par default
			$this->fieldsConfig = unserialize($strConfig);
		}else{
			$this->fieldsConfig = $this->objDef->getDefaultFieldsListConfig($this->className);
		}

		$this->hide = $this->config['hide'];
		$this->add = $this->config['add'];
		foreach ($this->fieldsConfig as $fieldName => $show) {
			if($show){
				$this->add[] = $fieldName;
			}else{
				$this->hide[] = $fieldName;
			};
		}
		
		$orderColArray=explode('*',$orderByCol);
		$funcs = new Util();
		$q = $this->objDef->getBaseQuery($this->hide,array_merge($this->add,array()),array($orderColArray,array('id',$orderColArray[1])));
		
		if($this->view=='mesdossiersencours'){
			$q->addWhere('a.demandeur_id = ?', $this->personne->id);
			$q->addWhere('a.deleted = false');
			$q->addWhere('a.brouillon = false');
			$q->addWhere('a.ferme = false');
		}elseif($this->view=='atraiter'){
			$q->addWhere('a.deleted = false');
			$q->addWhere('a.brouillon = false');
			$q->addWhere('a.ferme = false');
			$q->innerJoin('a.linketatdossier le')
			->where('le.id = (SELECT max(l2.id) AS maxid from linketatdossier l2 where l2.dossier_id = a.id )')
			->innerJoin('le.acteur g')
			->innerJoin('g.personnes ig')
			->addWhere('ig.personne_id = ?', $this->personne->id);
		}elseif($this->view=='archive'){
			$q->innerJoin('a.linketatdossier le');
			$q->addWhere('le.personne_id = ?', $this->personne->id); //toutes les personnes qu'on traité ce dossier
			$q->addWhere('a.deleted = false');
			$q->addWhere('a.brouillon = false');
		}elseif($this->view=='all'){
			$q->addWhere('a.deleted = false');
			$q->addWhere('a.brouillon = false');
		}else{
			//C'est une action unconnue! ->filtre pour eviter d'afficher des dossiers qu'il faut pas.
			$q->addWhere('a.id = -1');
		}
		
		$search->addFilterstoQuery($q);
		$q->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY);
		
		$this->pager = $search->setPager($request, $q, $this->nbPosts, $nbpage);
		$this->totalRecords = $this->pager->getNbResults();
		Util::sendDebug('aca executa', 2);
		$this->records = $this->pager->getResults(); //$q->execute();

		if($this->nbPosts!='')
		$this->personne->setParam($this->view.'/NbResults', $this->nbPosts);
		if($orderByCol!='')
		$this->personne->setParam($this->view.'/OrderColumn', $orderByCol);
		$this->orderByCol = $orderByCol;
		
	  	
	  	 return $this->renderPartial('listDossiers');
	  }
  	  
	  public function executeGetListNotifications(sfWebRequest $request){
	  	$this->personne = $this->getUser()->getUserProfile();
	  	$q = Doctrine_Query::create()->from('notification n')
	  			->where('n.viewed = false')
	  			->innerJoin('n.message m WITH n.evenementclass = ? ', 'message')
	  			->leftJoin('m.service s')
	  			->addOrderBy('s.description')
	  			->addWhere('n.personne_id = ?', $this->personne['id']);
		$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);  			
	  	$this->messages = $q->execute();
	  	
	  	$this->personne = $this->getUser()->getUserProfile();
	  	$q = Doctrine_Query::create()->from('notification n')
	  			->where('n.viewed = false')
	  			->innerJoin('n.dossier m WITH n.evenementclass = ? ', 'dossier')
	  			->innerJoin('m.service s')
	  			->addOrderBy('s.description, m.id')
	  			->addWhere('n.personne_id = ?', $this->personne['id']);
		$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);  			
	  	$this->dossiers = $q->execute();
	  		  	
	  	return $this->renderPartial('listNotifications');
	  }
	  
	  public function executeGetNotificationContent(sfWebRequest $request){
  	  	$this->notification = Doctrine::getTable('notification')->find($request->getParameter('id'));
  	  	if($this->notification){
  	  		$this->objDef = new objectDef($this->notification->evenementclass);
			$this->object = Doctrine::getTable($this->notification->evenementclass)->find($this->notification->evenement_id);
  	  		$this->notification->viewed = true; 
			$this->notification->save();
			return $this->renderPartial($this->notification->evenementclass.'Content');
  	  	}else{
  	  		 
  	  	}
  	  }
}